package com.ge.modules.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ge.common.annotation.LoginUserApi;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbUserConcernDto;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.api.service.TbTechnicianService;
import com.ge.modules.api.service.TbUserConcernService;
import com.ge.modules.api.service.TbUserService;
import com.ge.modules.entity.TbStoreEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import com.ge.modules.entity.TbUser;
import com.ge.modules.entity.TbUserConcernEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户关注表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-17 15:12:57
 */
@RestController
@RequestMapping("/api/user/userconcern")
public class TbUserConcernController {
    @Autowired
    private TbUserConcernService userConcernService;

    @Autowired
    private TbUserService userService;

    @Autowired
    private TbStoreService storeService;

    @Autowired
    private TbTechnicianService technicianService;

    /**
     * 查询我的关注列表 根据不同的用户类型查询不同的关注用户
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) {
        params.put("userId", tbUser.getUserId());
        //查询列表数据
        Query query = new Query(params);
        //得到用户类型
        String userTypes = (String) params.get("userType");
        Integer userType = Integer.valueOf(userTypes);

        List<TbUserConcernDto> tbUserConcernDtoList =new ArrayList<>();
        List<TbUserConcernEntity> userConcernList = userConcernService.queryList(query);
        //关注类型 1关注用户 2关注技师 3关注店铺
        //根据不同的用户类型 封装不同的数据
        if(userType == 1){
            for (int i = 0; i < userConcernList.size(); i++) {
                TbUserConcernDto tbUserConcernDto = userConcernList.get(i).toDto();
                //查询用户信息
                TbUser user = userService.queryById(tbUserConcernDto.getByUserId());
                tbUserConcernDto.setSex(user.getSex());
                tbUserConcernDto.setNickName(user.getNickname());
                tbUserConcernDto.setUserImgUrl(user.getHeadPicture());
                tbUserConcernDtoList.add(tbUserConcernDto);
            }
        }else if(userType == 2){
            for (int i = 0; i < userConcernList.size(); i++) {
                TbUserConcernDto tbUserConcernDto = userConcernList.get(i).toDto();
                //查询技师信息
                TbTechnicianEntity tbTechnicianEntity = technicianService.queryByTechnicianId(tbUserConcernDto.getByUserId());
                tbUserConcernDto.setSex(tbTechnicianEntity.getSex());
                tbUserConcernDto.setNickName(tbTechnicianEntity.getNickname());
                tbUserConcernDto.setUserImgUrl(tbTechnicianEntity.getHeadpicture());
                tbUserConcernDto.setTechnicianorderCount(tbTechnicianEntity.getOrderCount());
                tbUserConcernDto.setGrade(tbTechnicianEntity.getGrade());
                tbUserConcernDtoList.add(tbUserConcernDto);
            }
        }else if(userType == 3){
            for (int i = 0; i < userConcernList.size(); i++) {
                TbUserConcernDto tbUserConcernDto = userConcernList.get(i).toDto();
                //查询店铺信息
                TbStoreEntity tbStoreEntity = storeService.queryByStoreId(tbUserConcernDto.getByUserId());
                tbUserConcernDto.setNickName(tbStoreEntity.getStoreName());
                tbUserConcernDto.setUserImgUrl(tbStoreEntity.getBackgroundPicture());
                tbUserConcernDto.setStoreStar(tbStoreEntity.getStar());
                tbUserConcernDto.setStoreAverageExpend(tbStoreEntity.getAverageExpend());
                tbUserConcernDtoList.add(tbUserConcernDto);
            }
        }
        int total = userConcernService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbUserConcernDtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("userconcern:info")
    public R info(@PathVariable("id") Integer id) {
        TbUserConcernEntity userConcern = userConcernService.queryObject(id);

        return R.ok().put("data", userConcern);
    }

    /**
     * 添加 or 取消用户关注
     */
    @PostMapping("/save")
    public R save(@LoginUserApi TbUser tbUser, Integer userType, Integer userId) {
        //查询当前用户是否关注过该用户
        TbUserConcernEntity tbUserConcernEntity = new TbUserConcernEntity();
        tbUserConcernEntity.setUserType(userType);
        tbUserConcernEntity.setByUserId(userId);
        tbUserConcernEntity.setUserId(tbUser.getUserId());
        //关注类型 0 未关注 1 已关注
        int type = 0;
        //未关注 添加关注 如果关注了就取消关注
        TbUserConcernEntity userConcernEntity = userConcernService.queryByObject(tbUserConcernEntity);
        if (userConcernEntity == null) {
            tbUserConcernEntity.setCreateTime(new Date());
            userConcernService.save(tbUserConcernEntity);
            type = 1;
        } else {
            userConcernService.delete(userConcernEntity.getId());
        }
        return R.ok().put("data",type);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbUserConcernEntity userConcern) {
        userConcernService.update(userConcern);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        userConcernService.deleteBatch(ids);

        return R.ok();
    }

}
